﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace SCOSub
{

    public partial class UpLowChange : Form
    {
        public delegate void SetMainFormTopMostHandle(bool topmost);
        public event SetMainFormTopMostHandle SetMainFormTopMost;//设置MainForm的TopMost

        public UpLowChange()
        {
            InitializeComponent();
            this.TopMost = Const.DoTopMost;

        }
        private void UpLowChange_FormClosed(object sender, FormClosedEventArgs e)
        {
            SetMainFormTopMost(true);
        }
        private const string RX_toUpper = @"([}])([^\{\n]*)([\n\{])";
        private const string RX_toLower = @"([}])([^\{\n]*)([\n\{])";
        private const string RX_toFirstUpper = @"([}])([^a-zA-Z\{]*)([a-zA-Z]{1})([^\{\n]*)([\n\{])";

        private int mode = 0;//0->大写  1->小写 2->首字母大写

        private string Convert2(string im, string regex, MatchEvaluator ME)
        {
            im += "\n";
            Regex ra = new Regex(regex);
            Match ma = ra.Match(im);
            im = Regex.Replace(im, ra.ToString(), ME);
            return im;
        }
        private string Correct(Match m)
        {
            switch (mode)
            {
                case 0:
                    return m.Groups[1].Captures[0].Value + m.Groups[2].Captures[0].Value.ToUpper() + m.Groups[3].Captures[0].Value;
                case 1:
                    return m.Groups[1].Captures[0].Value + m.Groups[2].Captures[0].Value.ToLower() + m.Groups[3].Captures[0].Value;
                case 2:
                    return m.Groups[1].Captures[0].Value + m.Groups[2].Captures[0].Value + m.Groups[3].Captures[0].Value.ToUpper() + m.Groups[4].Captures[0].Value.ToLower() + m.Groups[5].Captures[0].Value;
            }
            return null;
        }

        #region 全大写

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            mode = 0;
            richTextBox1.Text = Convert2(richTextBox1.Text, RX_toUpper, new MatchEvaluator(Correct));
        }

        #endregion

        #region 全小写
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            mode = 1;
            richTextBox1.Text = Convert2(richTextBox1.Text, RX_toLower, new MatchEvaluator(Correct));
        }

        #endregion

        #region 首字母大写
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            mode = 2;
            richTextBox1.Text = Convert2(richTextBox1.Text, RX_toFirstUpper, new MatchEvaluator(Correct));
        }

        #endregion


        #region  复制等菜单

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string s = richTextBox1.SelectedText;
            if (s.Length == 0) return;
            Clipboard.SetText(s);
        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string s = richTextBox1.SelectedText;
            if (s.Length == 0) return;
            richTextBox1.SelectedText = "";
            Clipboard.SetText(s);
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = Clipboard.GetText();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = "";
        }
        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }
        #endregion

        

        


    }
}
